package com.purong.purong.util;

import android.content.Context;
import android.util.Log;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;

/**
 * Created by wenjiezeng on 2017/9/19.
 */
public class AmapLocation implements AMapLocationListener {
//    private static AmapLocation instance=null;
    private Context mContext;
    private AMapLocationClient mLocationClient = null;
    //声明AMapLocationClientOption对象
    private AMapLocationClientOption mLocationOption = null;
    private OnLocationListener mListener;
    private int maxLocationCount=10;
    private int hasLocationCount;
//    public static AmapLocation getInstance(Context context){
//        if (instance==null){
//            instance=new AmapLocation(context);
//        }
//        return instance;
//    }
    private boolean isStartLocation;
    public AmapLocation(Context context){
        this.mContext=context;
        initLocation();
    };
    public void setMaxLocationCount(int count){
        this.maxLocationCount=count;
    }
    public void setOnLocationListener(OnLocationListener listener){
        this.mListener=listener;
    }
    private void initLocation(){
        //初始化定位
        mLocationClient = new AMapLocationClient(mContext);
        //设置定位回调监听
        mLocationClient.setLocationListener(this);
        mLocationOption = new AMapLocationClientOption();
        //设置定位模式为AMapLocationMode.Hight_Accuracy，高精度模式。
        mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
        //获取一次定位结果：
        //该方法默认为false。
//         mLocationOption.setOnceLocation(true);

        //获取最近3s内精度最高的一次定位结果：
        //设置setOnceLocationLatest(boolean b)接口为true，启动定位时SDK会返回最近3s内精度最高的一次定位结果。如果设置其为true，setOnceLocation(boolean b)接口也会被设置为true，反之不会，默认为false。
//          mLocationOption.setOnceLocationLatest(true);
        //设置是否返回地址信息（默认返回地址信息）
        mLocationOption.setNeedAddress(true);
        //设置是否允许模拟位置,默认为true，允许模拟位置
        mLocationOption.setMockEnable(true);
        //设置定位间隔,单位毫秒,默认为2000ms，最低1000ms。
        mLocationOption.setInterval(3000);
        //单位是毫秒，默认30000毫秒，建议超时时间不要低于8000毫秒。
        mLocationOption.setHttpTimeOut(20000);
        //关闭缓存机制
        // mLocationOption.setLocationCacheEnable(false);

        //给定位客户端对象设置定位参数
        mLocationClient.setLocationOption(mLocationOption);
        //启动定位
    }
    public boolean isStartLocation(){
        return isStartLocation;
//        if (mLocationClient!=null){
//            return mLocationClient.isStarted();
//        }else {
//            return false;
//        }
    }
    public void startLocation(){

        if (mLocationClient!=null){
            mLocationClient.startLocation();
            isStartLocation=true;
        }

    }
    public void stopLocation(){

        if (mLocationClient!=null){
            mLocationClient.stopLocation();
            isStartLocation=false;
            hasLocationCount=0;
        }
    }
    public void destroy(){
        if (mLocationClient!=null){
            mLocationClient.onDestroy();
            isStartLocation=false;
        }

    }
    @Override
    public void onLocationChanged(AMapLocation amapLocation) {
        if (amapLocation != null) {
            if (amapLocation.getErrorCode() == 0) {
                if (mListener!=null){
//                    Log.e("zwj","........onLocationSuccess"+hasLocationCount);
                    mListener.onLocationSuccess(amapLocation);
                }
            }else {
                if (mListener!=null){
                    mListener.onLocationFail(amapLocation.getErrorCode());
                }
                Log.e("zwj","........onLocationChanged,location Error");
                //定位失败时，可通过ErrCode（错误码）信息来确定失败的原因，errInfo是错误信息，详见错误码表。
                Log.e("zwj","location Error, ErrCode:"
                        + amapLocation.getErrorCode() + ", errInfo:"
                        + amapLocation.getErrorInfo());
            }
        }
        hasLocationCount++;
        if (hasLocationCount>=maxLocationCount){
            stopLocation();
            if (mListener!=null){
                mListener.onReachMaxCount();
            }
        }

    }

    public interface OnLocationListener{
         void onLocationSuccess(AMapLocation aMapLocation);
        void onLocationFail(int code);
        void onReachMaxCount();
    }
}
